home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Property Editors / dsattrs.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  4KB  |  137 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {       Dataset Designer Save Attributes Dialog         }
  6. {                                                       }
  7. {       Copyright (c) 1997,99 Inprise Corporation       }
  8. {                                                       }
  9. {*******************************************************}
  10.  
  11. unit DSAttrS;
  12.  
  13. interface
  14.  
  15. uses
  16.   SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms,
  17.   Dialogs, ExtCtrls, StdCtrls, DRIntf, LibHelp;
  18.  
  19. type
  20.   TSaveAttributesAs = class(TForm)
  21.     BasedOnList: TComboBox;
  22.     AttributeNameEdit: TEdit;
  23.     Label1: TLabel;
  24.     Label2: TLabel;
  25.     OKBtn: TButton;
  26.     Bevel1: TBevel;
  27.     CancelBtn: TButton;
  28.     HelpBtn: TButton;
  29.     procedure OKBtnClick(Sender: TObject);
  30.     procedure CancelBtnClick(Sender: TObject);
  31.     procedure HelpBtnClick(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.   private
  34.     procedure AddToList(const Value: string);
  35.   public
  36.     function Execute(const TableName, FieldName: string;
  37.       var AttributesName: string; var BasedOnID: TAttrID): Boolean;
  38.   end;
  39.  
  40. function SaveAttributesAsDlg(const TableName, FieldName: string; var Name: string;
  41.   var BasedOnID: TAttrID): Boolean;
  42.  
  43. implementation
  44.  
  45. uses DsnDBCst;
  46.  
  47. function SaveAttributesAsDlg(const TableName, FieldName: string; var Name: string;
  48.   var BasedOnID: TAttrID): Boolean;
  49. var
  50.   SaveAttributesAsForm: TSaveAttributesAs;
  51. begin
  52.   SaveAttributesAsForm := TSaveAttributesAs.Create(Application);
  53.   try
  54.     Result := SaveAttributesAsForm.Execute(TableName, FieldName, Name, BasedOnID);
  55.   finally
  56.     SaveAttributesAsForm.Free;
  57.   end;
  58. end;
  59.  
  60. {$R *.DFM}
  61.  
  62. procedure TSaveAttributesAs.OKBtnClick(Sender: TObject);
  63. var
  64.   AttrID: TAttrID;
  65. begin
  66.   AttrID := FindAttrID(AttributeNameEdit.Text);
  67.   if not IsNullID(AttrID) then
  68.     raise Exception.CreateResFmt(@SDSAttrExists, [AttributeNameEdit.Text]);
  69.   ModalResult := mrOk;
  70. end;
  71.  
  72. procedure TSaveAttributesAs.CancelBtnClick(Sender: TObject);
  73. begin
  74.   ModalResult := mrCancel;
  75. end;
  76.  
  77. procedure TSaveAttributesAs.HelpBtnClick(Sender: TObject);
  78. begin
  79.   Application.HelpContext(HelpContext);
  80. end;
  81.  
  82. procedure TSaveAttributesAs.AddToList(const Value: string);
  83. begin
  84.   BasedOnList.Items.Add(Value);
  85. end;
  86.  
  87. procedure TSaveAttributesAs.FormCreate(Sender: TObject);
  88. begin
  89.   BasedOnList.Items.Add(SDSNone);
  90.   GetAttrNames(AddToList);
  91.   HelpContext := hcDSaveAttributesAs;
  92. end;
  93.  
  94. function TSaveAttributesAs.Execute(const TableName, FieldName: string;
  95.   var AttributesName: string; var BasedOnID: TAttrID): Boolean;
  96. var
  97.   Index: Integer;
  98.  
  99.   function UniqueAttrName(const TableName, FieldName: string): string;
  100.   var
  101.     Template: string;
  102.     I: Integer;
  103.     AttrID: TAttrID;
  104.   begin
  105.     Template := TableName;
  106.     I := Pos('.', Template);
  107.     if I > 0 then Delete(Template, I, MaxInt);
  108.     Template := Template + FieldName;
  109.     for I := Length(Template) downto 1 do
  110.       if Template[I] = ' ' then Delete(Template, I, 1);
  111.     I := 0;
  112.     Result := Template;
  113.     while True do
  114.     begin
  115.       AttrID := FindAttrID(Result);
  116.       if IsNullID(AttrID) then Exit;
  117.       Inc(I);
  118.       Result := Format('%s%d', [Template, I]);
  119.     end;
  120.   end;
  121.   
  122. begin
  123.   Caption := Format(Caption, [FieldName]);
  124.   Index := BasedOnList.Items.IndexOf(GetAttrName(BasedOnID));
  125.   if Index < 0 then Index := 0;
  126.   BasedOnList.ItemIndex := Index;
  127.   AttributeNameEdit.Text := UniqueAttrName(TableName, FieldName);
  128.   Result := ShowModal = mrOk;
  129.   if Result then
  130.   begin
  131.     AttributesName := AttributeNameEdit.Text;
  132.     BasedOnID := FindAttrID(BasedOnList.Text);
  133.   end;
  134. end;
  135.  
  136. end.
  137.